//
//    Copyright (C) 2012  OinkStudio
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.

package oldClasses;

import java.util.ArrayList;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;


public class TableModel extends AbstractTableModel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 7790479725521757002L;

	public static final int HIDDEN_INDEX = 0;

	private ArrayList<String> columnNames;
	protected Vector<Object> dataVector;

	public TableModel(ArrayList<String> columnNames) {
		this.columnNames = columnNames;
		dataVector = new Vector<Object>();
	}

	public String getColumnName(int column) {
		return (String) columnNames.get(column);
	}

	public boolean isCellEditable(int row, int column) {
		if (column == HIDDEN_INDEX)
			return false;
		else
			return true;
	}

	/*
	 * public Class getColumnClass(int column) { switch (column) { case
	 * TITLE_INDEX: case ARTIST_INDEX: case ALBUM_INDEX: return String.class;
	 * default: return Object.class; } }
	 */

	public Object getValueAt(int row, int column) {
		//Esto se puede hacer porque dataVector.get(row) devuelve un array de objetos, que es lo que espera
		//el constructor de TableInfo
		TableInfo record = (TableInfo) dataVector.get(row);
		switch (column) {
		case 0:
			return record.getCamp1();
		case 1:
			return record.getCamp2();
		case 2:
			return record.getCamp3();
		case 3:
			return record.getCamp4();
		case 4:
			return record.getCamp5();
		case 5:
			return record.getCamp6();
		case 6:
			return record.getCamp7();
		case 7:
			return record.getCamp8();
		default:
			return new Object();
		}
	}

	/**
	 * aqui s'hauria de agafar un Objecte, mirar de quin tipus es i dp fer el
	 * set value amb l'objecte (Number, String o Date)
	 **/
	public void setValueAt(Object valueObj, int row, int column) {
		TableInfo record = (TableInfo) dataVector.get(row);
		Object value = valueObj;
		switch (column) {
		case 0:
			record.setCamp1(value.toString());
			break;
		case 1:
			record.setCamp2(value.toString());
			break;
		case 2:
			record.setCamp3(value.toString());
			break;
		case 3:
			record.setCamp4(value.toString());
			break;
		case 4:
			record.setCamp5(value.toString());
			break;
		case 5:
			record.setCamp6(value.toString());
			break;
		case 6:
			record.setCamp7(value.toString());
			break;
		case 7:
			record.setCamp8(value.toString());
			break;
		default:
			System.out.println("invalid index");
		}
		fireTableCellUpdated(row, column);

	}

	public int getRowCount() {
		return dataVector.size();
	}

	public int getColumnCount() {
		return columnNames.size();
	}

	public boolean hasEmptyRow() {
		if (dataVector.size() == 0)
			return false;
		TableInfo record = (TableInfo) dataVector.get(dataVector.size() - 1);
		if (record.getCamp1() == (null) && record.getCamp2() == (null)
				&& record.getCamp3() == (null) && record.getCamp4() == (null)
				&& record.getCamp5() == (null) && record.getCamp6() == (null)
				&& record.getCamp7() == (null) && record.getCamp8() == (null)) {
			return true;
		} else
			return false;
	}

	public void addEmptyRow() {
		dataVector.add(new TableInfo());
		fireTableRowsInserted(dataVector.size() - 1, dataVector.size() - 1);
	}
}